package logic

import (
	"context"
	"profile/pkg"
	"profile/userrpc/userrpc"
	"strconv"

	"profile/api/internal/svc"
	"profile/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserUpLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpLogic {
	return &UserUpLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserUpLogic) UserUp(req *types.UserUpRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	up, err := l.svcCtx.UserRpc.UserUP(l.ctx, &userrpc.UserUPRequest{
		Username: req.Username,
		Password: req.Password,
	})
	if err != nil {
		return &types.Response{
			Code:    0,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}

	handler, err := pkg.TokenHandler(strconv.FormatInt(up.Id, 10))
	if err != nil {
		return &types.Response{
			Code:    0,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Code:    200,
		Message: "用户登录成功",
		Data:    handler,
	}, nil
}
